package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.LoginVo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.service.util.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author MikeCoder
 * @create 2023-12-0118:28
 * @description:
 * @verson:
 */
@RestController
@RequestMapping("/admin/system/index")
@Tag(name = "首页接口")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    ValidateCodeService validateCodeService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo login = sysUserService.login(loginDto);
        return Result.build(login, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/generateValidateCode")
    @Operation(summary = "获取验证码")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }
 //localhost:8501/admin/system/index/getUserInfo
    //           /admin/system/index/getUserInfo
    @GetMapping("/getUserInfo")
    @Operation(summary = "用户信息")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token){
        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/logout")
    @Operation(summary = "退出登录")
    public Result logout(@RequestHeader(name = "token") String token){
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
